VERSION 5.00
Begin VB.Form frmStackTest 
   Caption         =   "VB Stack Test"
   ClientHeight    =   4950
   ClientLeft      =   120
   ClientTop       =   450
   ClientWidth     =   9675
   LinkTopic       =   "Form1"
   ScaleHeight     =   4950
   ScaleWidth      =   9675
   StartUpPosition =   3  'Windows Default
   Begin VB.Frame fmeHasElement 
      Caption         =   "Has StringData"
      Height          =   855
      Left            =   120
      TabIndex        =   27
      Top             =   3960
      Width           =   6135
      Begin VB.CommandButton cmdDelete 
         Caption         =   "Delete Element"
         Enabled         =   0   'False
         Height          =   495
         Left            =   4560
         TabIndex        =   31
         Top             =   240
         Width           =   1455
      End
      Begin VB.CheckBox chkElementExists 
         Caption         =   "Exists?"
         Height          =   495
         Left            =   3720
         TabIndex        =   30
         Top             =   240
         Width           =   1215
      End
      Begin VB.CommandButton cmdCheckHasElement 
         Caption         =   "Check"
         Height          =   495
         Left            =   1920
         TabIndex        =   29
         Top             =   240
         Width           =   1215
      End
      Begin VB.TextBox txtHasElement 
         Height          =   285
         Left            =   240
         TabIndex        =   28
         Text            =   "txtHasElement"
         Top             =   360
         Width           =   1575
      End
   End
   Begin VB.CommandButton cmdCurItemNothing 
      Caption         =   "iterator null"
      Height          =   495
      Left            =   6360
      TabIndex        =   26
      Top             =   4200
      Width           =   1215
   End
   Begin VB.CommandButton cmdMixedLink 
      Caption         =   "Mixed Link"
      Height          =   495
      Left            =   8760
      TabIndex        =   25
      Top             =   4200
      Width           =   615
   End
   Begin VB.CommandButton cmdLink 
      Caption         =   "LinkIterator ToNext"
      Height          =   495
      Left            =   7680
      TabIndex        =   24
      Top             =   4200
      Width           =   1095
   End
   Begin VB.CommandButton cmdFirst 
      Caption         =   "Go"
      Height          =   315
      Left            =   7440
      TabIndex        =   20
      Top             =   3000
      Width           =   735
   End
   Begin VB.CommandButton cmdClear 
      Caption         =   "Clear"
      Height          =   495
      Left            =   120
      TabIndex        =   19
      Top             =   1920
      Width           =   1215
   End
   Begin VB.CommandButton cmdCopy 
      Caption         =   "Copy queue"
      Height          =   495
      Left            =   120
      TabIndex        =   18
      Top             =   1320
      Width           =   1215
   End
   Begin VB.CommandButton cmdRemoveStackItem 
      Caption         =   "Remove Selected"
      Height          =   495
      Left            =   8280
      TabIndex        =   15
      Top             =   3000
      Width           =   855
   End
   Begin VB.Frame fmeStackItems 
      Caption         =   "Items in the stack"
      Height          =   3855
      Left            =   3240
      TabIndex        =   10
      Top             =   120
      Width           =   6255
      Begin VB.CommandButton cmdReturnHasElement 
         Caption         =   "HasElement"
         Height          =   315
         Left            =   5040
         TabIndex        =   32
         Top             =   3480
         Width           =   975
      End
      Begin VB.CommandButton cmdLast 
         Caption         =   "Go"
         Height          =   315
         Left            =   4200
         TabIndex        =   21
         Top             =   3240
         Width           =   735
      End
      Begin VB.CheckBox chkLastStackItemSelected 
         Caption         =   "Last"
         Enabled         =   0   'False
         Height          =   255
         Left            =   3480
         TabIndex        =   17
         Top             =   3120
         Width           =   1215
      End
      Begin VB.CheckBox chkFirstStackItemSelected 
         Caption         =   "First"
         Enabled         =   0   'False
         Height          =   255
         Left            =   3480
         TabIndex        =   16
         Top             =   2880
         Width           =   1215
      End
      Begin VB.ListBox lstItemsByIterator 
         Height          =   2205
         Left            =   3360
         TabIndex        =   13
         Top             =   600
         Width           =   2655
      End
      Begin VB.ListBox lstItemsCode 
         Height          =   2790
         Left            =   360
         TabIndex        =   11
         Top             =   600
         Width           =   2655
      End
      Begin VB.Label lblItemsByStackIterator 
         AutoSize        =   -1  'True
         Caption         =   "By Stack Iterator:"
         Height          =   195
         Left            =   3480
         TabIndex        =   14
         Top             =   360
         Width           =   1230
      End
      Begin VB.Label lblItemsCode 
         AutoSize        =   -1  'True
         Caption         =   "By Code:"
         Height          =   195
         Left            =   480
         TabIndex        =   12
         Top             =   360
         Width           =   645
      End
   End
   Begin VB.OptionButton optObject 
      Caption         =   "Node"
      Height          =   495
      Left            =   1800
      TabIndex        =   9
      Top             =   480
      Width           =   1215
   End
   Begin VB.OptionButton optString 
      Caption         =   "String"
      Height          =   495
      Left            =   240
      TabIndex        =   8
      Top             =   480
      Width           =   1215
   End
   Begin VB.Frame fmeStats 
      Caption         =   "DS Information"
      Height          =   1335
      Left            =   120
      TabIndex        =   3
      Top             =   2520
      Width           =   2895
      Begin VB.Label lblRearDataCaption 
         AutoSize        =   -1  'True
         Caption         =   "Rear String:"
         Height          =   195
         Left            =   240
         TabIndex        =   23
         Top             =   960
         Width           =   840
      End
      Begin VB.Label lblRearData 
         AutoSize        =   -1  'True
         Caption         =   "_________"
         Height          =   195
         Left            =   1320
         TabIndex        =   22
         Top             =   960
         Width           =   1530
      End
      Begin VB.Label lblNumItems 
         AutoSize        =   -1  'True
         Caption         =   "_____"
         Height          =   195
         Left            =   1560
         TabIndex        =   7
         Top             =   240
         Width           =   450
      End
      Begin VB.Label lblNumItemsLabel 
         AutoSize        =   -1  'True
         Caption         =   "Number of Items:"
         Height          =   195
         Left            =   240
         TabIndex        =   6
         Top             =   240
         Width           =   1200
      End
      Begin VB.Label lblFrontData 
         AutoSize        =   -1  'True
         Caption         =   "_________"
         Height          =   195
         Left            =   1320
         TabIndex        =   5
         Top             =   600
         Width           =   1530
      End
      Begin VB.Label lbFrontDataLabel 
         AutoSize        =   -1  'True
         Caption         =   "Front String:"
         Height          =   195
         Left            =   240
         TabIndex        =   4
         Top             =   600
         Width           =   855
      End
   End
   Begin VB.CommandButton cmdPop 
      Caption         =   "dequeue"
      Height          =   375
      Left            =   1800
      TabIndex        =   2
      Top             =   1320
      Width           =   1215
   End
   Begin VB.CommandButton cmdEnqueue 
      Caption         =   "enqueue"
      Height          =   375
      Left            =   1800
      TabIndex        =   1
      Top             =   120
      Width           =   1215
   End
   Begin VB.TextBox txtAddItem 
      Height          =   285
      Left            =   120
      TabIndex        =   0
      Text            =   "txtAddItem"
      Top             =   120
      Width           =   1575
   End
End
Attribute VB_Name = "frmStackTest"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private vm_Stack_obj As VBVariantQueue.Queue


Private Sub chkElementExists_Click()
cmdDelete.Enabled = (chkElementExists.Value = vbChecked)
End Sub

Private Sub cmdCheckHasElement_Click()
If vm_Stack_obj.hasElement(txtHasElement.Text) Then
    chkElementExists.Value = vbChecked
Else
    chkElementExists.Value = vbUnchecked
End If
End Sub

Private Sub cmdClear_Click()
vm_Stack_obj.Clear
clearCodeList
updateInterface
End Sub

Private Sub clearCodeList()
lstItemsCode.Clear
End Sub

Private Sub cmdCopy_Click()
Set vm_Stack_obj = vm_Stack_obj.Clone
updateInterface
MsgBox "Cloned. Interface should have not changed."
End Sub

Private Sub cmdCurItemNothing_Click()
Dim vl_queue As Queue
Dim vl_QueueIterator As LLIterator

Set vl_queue = New Queue
Set vl_QueueIterator = vl_queue.getIterator

MsgBox "On a queue with no items, LLIterator.currentItem returns " & vl_QueueIterator.hasCurrent
End Sub

Private Sub cmdDelete_Click()
MsgBox vm_Stack_obj.removeElementAndReturnSuccess(txtHasElement.Text)
updateIteratorList
cmdCheckHasElement_Click
End Sub

Private Sub cmdFirst_Click()
'Perform both operations to test
Dim vl_CopiedStackNodesIterator As LLIterator

Set vl_CopiedStackNodesIterator = vm_Stack_obj.getIterator

MsgBox "Copy at first: " & CStr(Not vl_CopiedStackNodesIterator.hasPrevious) & vbCrLf & _
        "The program will now attempt to move both iterators to the last item."

vl_CopiedStackNodesIterator.moveFirst
End Sub

Private Sub cmdLast_Click()
Dim vl_OriginalStackNodesIterator As LLIterator
Dim vl_CopiedStackNodesIterator As LLIterator

Set vl_CopiedStackNodesIterator = vm_Stack_obj.getIterator

MsgBox "Copy at last: " & CStr(Not vl_CopiedStackNodesIterator.hasNext) & vbCrLf & _
        "The program will now attempt to move both iterators to the last item."

vl_CopiedStackNodesIterator.moveLast

End Sub

Private Sub cmdLink_Click()
Dim vl_Iterator As LLIterator
Set vl_Iterator = vm_Stack_obj.getIterator

vl_Iterator.linkIteratorWithStart vm_Stack_obj.getIterator

updateIteratorListByIterator vl_Iterator
End Sub

Private Sub cmdPop_Click()
vm_Stack_obj.dequeue
modifyListOfPushedOrPopped True
updateInterface
resetForm
End Sub

Private Sub cmdEnqueue_Click()
addStringToStack txtAddItem.Text & IIf(optObject.Value, " (O)", vbNullString)
modifyListOfPushedOrPopped False
updateInterface
resetForm
End Sub


Private Sub addStringToStack(ByVal pr_StringToAdd As String)
If optObject.Value = True Then
    addStringToStackAsObject pr_StringToAdd
Else
    addStringToStackAsString pr_StringToAdd
End If
End Sub

Private Sub addStringToStackAsString(ByVal pr_StringToAdd_str As String)
vm_Stack_obj.enqueue pr_StringToAdd_str
End Sub

Private Sub addStringToStackAsObject(ByVal pr_StringToAdd_str As String)
Dim vl_Object_obj As LLNode

Set vl_Object_obj = New LLNode
vl_Object_obj.initWithData pr_StringToAdd_str

vm_Stack_obj.enqueue vl_Object_obj
End Sub


Private Sub modifyListOfPushedOrPopped(ByVal pr_Popped_bln As Boolean)
If pr_Popped_bln Then
    removeFirstItemFromList
Else
    addFirstItemToList getTextFromObject(vm_Stack_obj.rear)
End If

updateIteratorList
End Sub

Private Sub removeFirstItemFromList()
lstItemsCode.RemoveItem (0)
End Sub

Private Sub addFirstItemToList(ByVal pr_ItemToAdd_str As String)
lstItemsCode.AddItem pr_ItemToAdd_str, lstItemsCode.ListCount
End Sub

Private Sub updateIteratorList()
Dim vl_LLIterator As LLIterator
Set vl_LLIterator = vm_Stack_obj.getIterator

updateIteratorListByIterator vl_LLIterator
End Sub

Private Sub updateIteratorListByIterator(ByVal pr_Iterator As LLIterator)

lstItemsByIterator.Clear

pr_Iterator.moveFirst

Do While pr_Iterator.hasCurrent
    addVariantToList pr_Iterator.currentItem
    
    pr_Iterator.moveNext
Loop
End Sub

Private Sub addVariantToList(ByVal pr_VariantToAdd As Variant)
If IsObject(pr_VariantToAdd) Then
    lstItemsByIterator.AddItem findStringInNode(pr_VariantToAdd).Data
Else
    lstItemsByIterator.AddItem pr_VariantToAdd
End If
End Sub

Private Function findStringInNode(startingNode As Variant) As LLNode
Dim vl_FindObjectVariantString As LLNode

Set vl_FindObjectVariantString = startingNode
    
Do Until VarType(vl_FindObjectVariantString.Data) = vbString
    Set vl_FindObjectVariantString = vl_FindObjectVariantString.nextItem
Loop

Set findStringInNode = vl_FindObjectVariantString
End Function


Private Sub cmdRemoveStackItem_Click()
If lstItemsByIterator.ListIndex >= 0 Then
    lstItemsCode.RemoveItem lstItemsByIterator.ListIndex
    
    vm_Stack_obj.removeElementAndReturnByIndex lstItemsByIterator.ListIndex
    
    updateIteratorList
    updateInterface
End If
End Sub

Private Sub moveIfHasNextOrAlert(ByVal iterator As LLIterator)
If iterator.hasNext Then
    iterator.moveNext
Else
    lstItemsByIterator.BackColor = &HFF0000
End If
End Sub

Private Sub cmdMixedLink_Click()
Dim vl_Queue1 As Queue
Dim vl_Queue2 As Queue

Dim vl_Queue1Iterator As LLIterator

Set vl_Queue1 = New Queue
Set vl_Queue2 = New Queue

vl_Queue2.enqueue "item1"
vl_Queue2.enqueue "item2"

Set vl_Queue1Iterator = vl_Queue1.getIterator
vl_Queue1Iterator.linkIteratorWithStart vl_Queue2.getIterator
updateIteratorListByIterator vl_Queue1Iterator

Set vl_Queue1Iterator = vl_Queue2.getIterator
vl_Queue1Iterator.linkIteratorWithStart vl_Queue1.getIterator
updateIteratorListByIterator vl_Queue1Iterator

Set vl_Queue1Iterator = vl_Queue1.getIterator
vl_Queue1Iterator.linkEndWithIterator vl_Queue2.getIterator
updateIteratorListByIterator vl_Queue1Iterator

Set vl_Queue1Iterator = vl_Queue2.getIterator
vl_Queue1Iterator.linkEndWithIterator vl_Queue1.getIterator
updateIteratorListByIterator vl_Queue1Iterator

End Sub

Private Sub cmdReturnHasElement_Click()
MsgBox vm_Stack_obj.hasElement(vm_Stack_obj.getElementByIndex(lstItemsByIterator.ListIndex)) & getTextFromObject(vm_Stack_obj.getElementByIndex(lstItemsByIterator.ListIndex))
End Sub

Private Sub Form_Initialize()
Set vm_Stack_obj = New Queue
End Sub

Private Sub Form_Load()
initializeForm
End Sub

Private Sub initializeForm()
resetForm
clearDataControls
End Sub

Private Sub resetForm()
txtAddItem.Text = vbNullString
optString.Value = True
End Sub


Private Sub clearDataControls()
lstItemsCode.Clear
lblNumItems.Caption = 0
lblFrontData.Caption = 0
End Sub

Private Sub tmrInterfaceUpdate_Timer()
updateInterface
End Sub

Private Sub updateInterface()

lblNumItems.Caption = vm_Stack_obj.numItems
lblFrontData.Caption = getFirstItemCaption
lblRearData.Caption = getLastItemCaption
End Sub

Private Function getFirstItemCaption()
Const CL_NODATA_STR As String = "<NO DATA>"
Dim vl_FirstItemCaption_str As String


If Not vm_Stack_obj.isQueueEmpty Then
    vl_FirstItemCaption_str = getTextFromObject(vm_Stack_obj.front)
Else
    vl_FirstItemCaption_str = CL_NODATA_STR
End If

getFirstItemCaption = vl_FirstItemCaption_str
End Function

Private Function getLastItemCaption()
Const CL_NODATA As String = "<NO DATA>"

Dim vl_LastItemCaption As String

If Not vm_Stack_obj.isQueueEmpty Then
    vl_LastItemCaption = getTextFromObject(vm_Stack_obj.rear)
Else
    vl_LastItemCaption = CL_NODATA
End If

getLastItemCaption = vl_LastItemCaption
End Function

Private Function getTextFromObject(ByVal pr_SrcVariant_var As Variant) As String
Dim vl_ItemCaption_str As String

Dim vl_PeekedNode__obj As LLNode


If IsObject(pr_SrcVariant_var) Then
    If Not IsEmpty(pr_SrcVariant_var) Then
        If TypeOf pr_SrcVariant_var Is LLNode Then
            Set vl_PeekedNode__obj = pr_SrcVariant_var
            vl_ItemCaption_str = vl_PeekedNode__obj.Data
        End If
    End If
Else
    vl_ItemCaption_str = pr_SrcVariant_var
End If

getTextFromObject = vl_ItemCaption_str
End Function


Private Sub Form_Terminate()
Set vm_Stack_obj = Nothing
End Sub

Private Sub lstItemsByIterator_Click()
Dim i As Integer
Dim vl_LLIterator As LLIterator

Set vl_LLIterator = vm_Stack_obj.getIterator


For i = 0 To lstItemsByIterator.ListIndex - 1
    vl_LLIterator.moveNext
Next i

If Not vl_LLIterator.hasNext Then chkLastStackItemSelected.Value = vbChecked Else chkLastStackItemSelected.Value = vbUnchecked
If Not vl_LLIterator.hasPrevious Then chkFirstStackItemSelected.Value = vbChecked Else chkFirstStackItemSelected.Value = vbUnchecked
cmdCheckHasElement.Enabled = (lstItemsByIterator.ListIndex <> -1)

End Sub

Private Sub txtAddItem_Change()
If txtAddItem.Text = vbNullString Then
    cmdEnqueue.Enabled = False
Else
    cmdEnqueue.Enabled = True
End If
End Sub

Private Sub txtAddItem_GotFocus()
cmdEnqueue.Default = True
End Sub

Private Sub txtHasElement_GotFocus()
cmdCheckHasElement.Default = True
End Sub
